package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.dto.OutboundDetailQueryDTO;
import com.intelligent.dto.OutboundOrderQueryDTO;
import com.intelligent.dto.WarehouseReceiptDetailQueryDTO;
import com.intelligent.pojo.OutboundDetail;
import com.intelligent.service.OutboundDetailService;
import com.intelligent.utils.Result;
import com.intelligent.vo.OutboundDetailVO;
import com.intelligent.vo.OutboundOrderVO;
import com.intelligent.vo.WarehouseReceiptDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 出库单控制器
 */
@RestController
@RequestMapping("/outbound/order")
public class OutboundOrderController {

    @Autowired
    private OutboundDetailService outboundDetailService;

    //分页查询
    @PostMapping("/page")
    public Result<IPage<OutboundOrderVO>> pageList(@RequestBody OutboundOrderQueryDTO query) {
        IPage<OutboundOrderVO> page = outboundDetailService.pageList(query);
        return Result.success(page);
    }

    //获取出库单详情
    @PostMapping("/getDetail")
    public Result<OutboundOrderVO> getDetail(@RequestParam("id") Long id) {
        OutboundDetail outboundDetail = outboundDetailService.getById(id);
        if (outboundDetail == null) {
            return Result.error("出库单不存在");
        }

        // 转换为VO对象
        OutboundOrderVO vo = new OutboundOrderVO();
        org.springframework.beans.BeanUtils.copyProperties(outboundDetail, vo);

        // 这里可以添加获取关联信息的逻辑，如产品、用户、单位等

        return Result.success(vo);
    }

    //新增
    @PostMapping
    public Result<Boolean> add(@RequestBody OutboundDetail outboundDetail) {
        try {
            boolean success = outboundDetailService.add(outboundDetail);
            return success ? Result.success(true) : Result.error("新增出库单失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @PostMapping("/pageOutbound")
    public Result<IPage<OutboundDetailVO>> pageList(@RequestBody OutboundDetailQueryDTO query) {
        IPage<OutboundDetailVO> page = outboundDetailService.pageDetailList(query);
        return Result.success(page);
    }
}
